<!DOCTYPE html>
<html lang="zh-cn">
<head>
	
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
	<title>
高亮Markdown中的代码（续） | 
穷折腾</title>
	<link rel="stylesheet" href="/static/css/style.css" />
	<link rel="stylesheet" href="/static/css/pygments.css" />
	<link rel="alternate" type="application/rss+xml" title="RSS" href="http://blog.zorro.im/rss.xml" />
	<link rel="icon" type="image/png" href="/static/img/favicon.png">
</head>
<body>
    <div id="container">
      <div id="main" role="main">
        <header>
		<h1>
高亮Markdown中的代码（续）
</h1>
		</header>

     <nav>
		<span><a title="home page" class="" href="/">home</a></span>
		<span><a title="标签" class="" href="/tags/index.html">标签</a></span>
		<span><a title="订阅" class="" href="/rss.xml">订阅</a></span>
        <span><a href="/posts/about.html" title="关于">关于</a></span>
     </nav>

     <article class="content">
        <section class="post">
            
		<p>之前写过一篇文章（<a href="/posts/how-to-highlight-code-in-markdown.html">高亮Markdown中的代码</a>)，介绍了python-markdown2中高亮代码的方法。后来在使用中我发现了一个<code>fenced code blocks</code>的bug，如果代码中间有空行，它会把换行之后的部分当成嵌套的。生成的文件中会有一堆类似这样的部分：</p>
<div class="codehilite"><pre><span class="nt">&lt;span</span> <span class="na">class=</span><span class="s">&quot;k&quot;</span><span class="nt">&gt;</span>class<span class="nt">&lt;/span&gt;</span> <span class="nt">&lt;span</span> <span class="na">class=</span><span class="s">&quot;nc&quot;</span><span class="nt">&gt;</span>
</pre></div>


<p>后来到其Github上一搜，发现很多人都有类似问题，比如<a href="https://github.com/trentm/python-markdown2/issues/109">这里</a>。也有人给出了<a href="https://github.com/trentm/python-markdown2/pull/117">解决方案</a>。</p>
<p>但是这个Pull request已经提出4个月了，原作者至今没有通过，而且整个代码也有近1年没有动过了。Clone了一份它的代码，发现如果想自定义扩展的话比较困难。无奈之下从新研究起了曾经被我抛弃的Python-Markdown。结果发现原来它也有很多扩展，而且比Markdown2中的更多。更重要的是提供了扩展的接口，很方便地写自己的扩展。于是就在peanut中替换了markdown2，代码如下：</p>
<div class="codehilite"><pre><span class="n">md</span> <span class="o">=</span> <span class="n">markdown</span><span class="o">.</span><span class="n">Markdown</span><span class="p">(</span><span class="n">extensions</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;fenced_code&#39;</span><span class="p">,</span> <span class="s">&#39;codehilite&#39;</span><span class="p">,</span> <span class="s">&#39;meta&#39;</span><span class="p">])</span>
<span class="c">#do something</span>
<span class="n">html</span> <span class="o">=</span> <span class="n">md</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="n">content</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s">&#39; </span><span class="se">\n</span><span class="s">&#39;</span><span class="p">))</span>
</pre></div>


<p><code>fenced_code</code>用来识别</p>
<div class="codehilite"><pre>```python
```
</pre></div>


<p>这样的代码段，<code>codehilite</code>用来高亮代码，<code>meta</code>用来识别文章信息，比如：</p>
<div class="codehilite"><pre>title: 高亮Markdown中的代码（续）
date: 2013-8-20
tag: python
     markdown
</pre></div>


<p>过度完毕，问题搞定~</p>
	</section>
	<section class="meta">
		<span class="tags">Tagged by 
			<a href="/tags/python.html">python</a>
			<a href="/tags/markdown.html">markdown</a>
		</span>

		<span class="time">&nbsp;<time datetime="2013-08-20">2013-08-20</time></span>
	</section>
	<!-- Duoshuo Comment BEGIN -->
<div class="ds-thread"></div>
<script type="text/javascript">
	var duoshuoQuery = {short_name:"zqqf16"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = 'http://static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0] 
		|| document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
</script>
<!-- Duoshuo Comment END -->

<hr/>


        </section>
     </article>
	 <div id="copy">&copy; Powered by <a href="https://github.com/zqqf16/zqqf16.github.com" title="Peanut">Peanut</a> | Themed by <a href="http://lhzhang.com" title="sext ii">sext ii</a></div>
      </div>
    </div> <!--! end of #container -->
	<script>
  		(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  			(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  		 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  			})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  		ga('create', 'UA-41282906-2', 'zorro.im');
  		ga('send', 'pageview');
	</script>
</body>
</html>
